import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import externalGlobals from 'rollup-plugin-external-globals'
import autoImport from 'unplugin-auto-import/vite'
import globals from './globals'

export default defineConfig(({ command, mode }: any): any => {
  const env = loadEnv(mode, process.cwd(), '')
  const { VITE_APP_ALIAS = '' } = env
  return {
    plugins: [
      vue({
        script: {
          defineModel: true,
        },
      }),
      autoImport({
        imports: [
          'vue',
          'vue-router',
          {
            '@idmy/core': ['usePage', 'sleep', 'useLoading', 'useCache', 'http', 'newError', 'Dialog', 'Modal', 'Message', 'dayjs', 'useDelete'],
          },
        ],
        dts: 'src/types/auto-imports.d.ts',
        dirs: ['src/types'],
      }),
    ],
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            'color-text-4': 'var(--color-neutral-6)',
          },
          javascriptEnabled: true,
        },
      },
    },
    base: '/' + VITE_APP_ALIAS,
    build: {
      target: 'es2015',
      outDir: 'dist/' + VITE_APP_ALIAS,
      rollupOptions: {
        external: Object.keys(globals),
        plugins: [externalGlobals(globals)],
      },
    },
    server: {
      host: '0.0.0.0',
      port: 9000,
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, '.', 'src'),
      },
    },
    assetsDir: '',
  }
})
